package com.atguigu.crowd.service.impl;

import com.atguigu.crowd.entity.Auth;
import com.atguigu.crowd.entity.AuthExample;
import com.atguigu.crowd.mapper.AuthMapper;
import com.atguigu.crowd.service.api.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthMapper authMapper;

    @Override
    public List<Auth> getAll() {
        // 直接传入一个new的AuthExample，查询全部的Auth
        return authMapper.selectByExample(new AuthExample());
    }

    @Override
    public List<Integer> getAssignAuthByRoleId(Integer roleId) {
        return authMapper.selectByRoleId(roleId);
    }

    @Override
    public void saveRoleAuthRelationship(Map<String, List<Integer>> map) {

        // 从map中获取roleId，authIdList
        List<Integer> roleIdList = map.get("roleId");
        Integer roleId = roleIdList.get(0);

        List<Integer> authIdList = map.get("authIdList");

        // 清除原有的关系信息
        authMapper.deleteOldRelationshipByRoleId(roleId);

        // 如果authIdList不为空，添加新的关系信息
        if (authIdList != null && authIdList.size() > 0 ){
            authMapper.insertNewRelationship(roleId,authIdList);
        }

    }

    @Override
    public List<String> getAssignAuthNameByAdminId(Integer adminId) {
        return authMapper.selectAssignAuthNameByAdminId(adminId);
    }
}
